<?php
class Smilies {
	private static $instance = NULL;
	
	private $smiliesInput = array();
	private $smiliesReplace = array();
	
	/**
	 * Konstruktor
	 */
	private function __construct() {}
	private function __clone() {}
	
	public static function getInstance() {
		if(self::$instance === null) {
			self::$instance = new Smilies;
			self::$instance->initSmilies();
		}
		return self::$instance;	
	}
	
	public function initSmilies() {
		$smilies = $this->getSmilies();
		foreach($smilies as $s) {
			$this->smiliesInput[] = $s['smilieInput'];
			$this->smiliesReplace[] = '<img src="'.Settings::getInstance()->getSetting("General.urlToCMS").'smilies/'.$s['smilieFile'].'" alt="'.$s['smilieInput'].'" />';
		}
	}

	
	public static function getSmilies() {
		$query = DB::getInstance()->query("
			SELECT
				id,
				smilieInput,
				smilieFile,
				smilieDisplay
			FROM
				".Tables::$smilieTbl."");
		$smilies = array();
		while($s = DB::getInstance()->fetchAssoc($query)) {
			$smilies[] = $s;
		}
		return $smilies;
	}

	
	public function getSmilie($id) {
		if(!is_numeric($id)) exit;
		
		$query = DB::getInstance()->query("
			SELECT
				id,
				smilieInput,
				smilieFile,
				smilieDisplay
			FROM
				".Tables::$smilieTbl."
			WHERE
				id = '".$id."'");
		$smilie = DB::getInstance()->fetchAssoc($query);
		return $smilie;
	}
	
	public function addSmilie($input,$smilieDisplay,$fileName) {
		if(!is_numeric($smilieDisplay)) exit;
		
		DB::getInstance()->query("
			INSERT INTO
				".Tables::$smilieTbl."
			SET
				smilieInput = '".DB::parseVar($input)."',
				smilieFile = '".DB::parseVar($fileName)."',
				smilieDisplay = '".$smilieDisplay."'");
	}
	
	public function editSmilie($id,$input,$smilieDisplay,$fileName) {
		if(!is_numeric($smilieDisplay) || !is_numeric($id)) exit;
		
		DB::getInstance()->query("
			UPDATE
				".Tables::$smilieTbl."
			SET
				smilieInput = '".DB::parseVar($input)."',
				smilieFile = '".DB::parseVar($fileName)."',
				smilieDisplay = '".$smilieDisplay."'
			WHERE
				id = '".$id."'");
	}
	
	public function deleteSmilie($id) {
		if(!is_numeric($id)) exit;
		
		$smilie = Smilies::getSmilie($id);
		if(file_exists("./smilies/".$smilie['smilieFile'])) {
			unlink("./smilies/".$smilie['smilieFile']);
		}
		DB::getInstance()->query("
			DELETE FROM
				".Tables::$smilieTbl."
			WHERE
				id = '".$id."'");
	}
	
	public function parse($text) {
		$text = str_replace($this->smiliesInput,$this->smiliesReplace,$text);
		return $text;
	}
	
	public function outputSmilies($varName) {
		$query = DB::getInstance()->query("
			SELECT
				id,
				smilieInput,
				smilieFile
			FROM
				".Tables::$smilieTbl."
			WHERE
				smilieDisplay = '0'");
		$smilies = array();
		while($s = DB::getInstance()->fetchAssoc($query)) {
			$smilies[] = $s;
		}
		
		General::getTpl()->assign('smilies',$smilies);
		General::getTpl()->assign($varName,
			General::getTpl()->fetch("smiliesForm.html"));
	}
	
	public function outputAllSmiliesPopup() {
		$query = DB::getInstance()->query("
			SELECT
				id,
				smilieInput,
				smilieFile
			FROM
				".Tables::$smilieTbl."");
		$smilies = array();
		while($s = DB::getInstance()->fetchAssoc($query)) {
			$smilies[] = $s;
		}
		
		General::getTpl()->assign('smilies',$smilies);
		
		General::getTpl()->assign('smilieForm',
			General::getTpl()->fetch("popupSmilies.html"));
		
		General::getTpl()->display("popupIndex.html");
	}
}
?>